from django.views import View
from django.http import JsonResponse
from django.contrib.auth.models import User
from .tasks import send_email
from app1.tasks import add
from django.db import transaction
import random
import string

# Create your views here.
random_str = string.ascii_letters + string.digits


def generate_random_string(self, length=9):
    random_string = ''.join(random.choice(random_str) for _ in range(length))
    return random_string

class App2View(View):
    def get(self, request):
        username = generate_random_string(3)
        pwd = generate_random_string(4)
        user = User.objects.create_user(username=username, password=pwd)
        add.delay(1, 2)
        send_email.delay(user.id)
        return JsonResponse({"username": username, "pwd": pwd, "id": user.id})
